﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Dummy
{
    public class GameData
    {
        static LoginWindow _loginWindow;
        static InviteWindow _inviteWindow;

        static GameClient _client;
        public static string gamerName { get; private set; }

        public static void StartClient(GameClient client)
        {
            _client = client;
            
            InitUI();
            GamerConnect();

            BattlefieldGameInfo.SendCurrentWordEvent += 
                new BattlefieldGameInfo.SendingCurrentWordDelagate(BattlefieldGameInfo_SendCurrentWordEvent); 
        }

        static void BattlefieldGameInfo_SendCurrentWordEvent(int moveI, int moveJ, string IJchar, string word)
        {
            _client.sendServiceClientCommand(GameClientCommand.gamermakemove, gamerName, 
                moveI.ToString(), moveJ.ToString(), IJchar, word);
        }

        static void InitUI()
        {
            _loginWindow = new LoginWindow();
            _loginWindow.HasCloseButton = false;
            _inviteWindow = new InviteWindow();
            _inviteWindow.HasCloseButton = false;
        }

        static void GamerConnect()
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _loginWindow.onLogin += (string gn) =>
                {
                    if (string.IsNullOrWhiteSpace(gn))
                        _loginWindow.ShowError("Gamer name cannot be empty");
                    else
                    {
                        _loginWindow.ProcessingLogin(true);
                        _client.sendServiceClientCommand(GameClientCommand.connectgamer, _loginWindow.gamerName);
                    }
                };
                _loginWindow.Show();
            });
        }

        public static void GamerConnected(string _gamerName)
        {
            gamerName = _gamerName;
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                BattlefieldGameInfo.SetGamerName(true, _gamerName);
                if (_loginWindow.Visibility == Visibility.Visible)
                    _loginWindow.Close();
                ShowInviteWindow();
            });
        }

        public static void GamerConnectError(string _error)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _loginWindow.ProcessingLogin(false);
                _loginWindow.ShowError(_error);
            });
        }

        static void ShowInviteWindow()
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _inviteWindow.onSendInvite += (string gn) =>
                {
                    if (string.IsNullOrWhiteSpace(gn))
                        _inviteWindow.ShowError("Please, select gamer to send invitation");
                    else
                    {
                        //_inviteWindow.ProcessingSendInvite(true);
                        _inviteWindow.ShowMessage("Invitation to gamer '" + gn + "' has been sent");
                        _client.sendServiceClientCommand(GameClientCommand.sendinvite, gamerName, gn);
                    }
                };
                _inviteWindow.Show();
                _client.sendServiceClientCommand(GameClientCommand.getgamerlist, gamerName);
            });
        }

        public static void UpdateGamerList(string[] gamerList)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _inviteWindow.Init(gamerList);
            });
        }

        public static void NewGamerCome(string newGamerName)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _inviteWindow.AddGamerToList(newGamerName);
            });
        }

        public static void SendInviteError(string gamerName, string _error)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _inviteWindow.ShowError(gamerName + " - " + _error);
            });
        }

        public static void GetInvite(string competitorName)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                string message = string.Format("User '{0}' invites you to play. Accept?", competitorName);
                if (MessageBox.Show(message, "Invitation", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    _client.sendServiceClientCommand(GameClientCommand.acceptinvite, gamerName, competitorName);
                }
            });
        }

        public static void StartGame(string competitorGamerName, bool yourMove, string startWord)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _inviteWindow.Close();
                BattlefieldGameInfo.SetGamerName(false, competitorGamerName);
                BattlefieldGameInfo.InitNewGame(startWord, yourMove);
            });
        }

        public static void StartGameError(string _gamerName, string _error)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                _inviteWindow.ShowError(_error);
            });
        }

        public static void GameWordList(bool isYourWords, string[] wordList)
        {
            
        }

        public static void GamerMakeMove(bool isYourMove, int moveI, int moveJ, string IJchar, string word, 
            int yourScore, int competitorScore)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                BattlefieldGameInfo.SetGamersScores(yourScore, competitorScore);
                BattlefieldGameInfo.AddWordToList(isYourMove, word);
                if (isYourMove)
                {
                    BattlefieldGameInfo.GiveMove();
                }
                else
                {
                    BattlefieldGameInfo.AddCharacterToBattlefield(moveI, moveJ, IJchar[0]);
                    BattlefieldGameInfo.TakeMove();
                }
                //BattlefieldGameInfo.EndSendingCurrentWord(true);
            });
        }

        public static void GamerMakeMoveError(string error)
        {
            BattlefieldGameInfo.SendUI((object obj) =>
            {
                BattlefieldGameInfo.SetErrorMessage(error);
                BattlefieldGameInfo.EndSendingCurrentWord(false);
            });
        }
    }

    public enum GameClientCommand
    {
        error,
        connectgamer,
        sendinvite,
        acceptinvite,
        getgamerlist,
        gamermakemove
    }
}
